#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int dx[4] = { 1 , -1 , 0 , 0 };
int dy[4] = { 0 , 0 , 1 , -1 };

int numEnclaves(vector<vector<int>>& grid) {
    int m = grid.size(), n = grid[0].size();
    int ret = 0;

    bool vis[510][510] = { false };
    queue<pair<int, int>> q;

    for (int i = 0; i < m; ++i)
    {
        if (grid[i][0])
        {
            q.push({ i,0 });
            vis[i][0] = true;
        }
        if (grid[i][n - 1])
        {
            q.push({ i,n - 1 });
            vis[i][n - 1] = true;
        }
    }


    for (int j = 0; j < n; ++j)
    {
        if (grid[0][j])
        {
            q.push({ 0,j });
            vis[0][j] = true;
        }
        if (grid[m - 1][j])
        {
            q.push({ m - 1,j });
            vis[m - 1][j] = true;
        }
    }

    while (q.size())
    {
        auto [a, b] = q.front();
        q.pop();

        for (int i = 0; i < 4; ++i)
        {
            int x = a + dx[i], y = b + dy[i];
            if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 && !vis[x][y])
            {
                q.push({ x,y });
                vis[x][y] = true;
            }
        }
    }

    for (int i = 0; i < m; ++i)
        for (int j = 0; j < n; ++j)
            if (grid[i][j] == 1 && !vis[i][j])
                ++ret;

    return ret;
}

int main()
{

	return 0;
}